package algorithm.search.binary.redundency;

import algorithm.sort.array.SortQuick;
import algorithm.sort.array.SortingMethod;

import java.util.Random;

/**
 * @program: beauty_of_data_structure_and_algotithm
 * @description: improved binary search
 * @author: Keyu Li
 * @create: 2021-09-01 22:19
 **/

public class TestSearchResult {
    final static int LOOP = 15;
    final static int ARRAY_NUM = 50;
    final static int INT_RANGE = ARRAY_NUM;

    public static void main(String[] args) {
        Random rd = new Random();
        int target;
        int[] index = new int[5];
        for (int i = 0; i < LOOP; i++) {
            int[] arr = createRandomArray();
            target = arr[rd.nextInt(ARRAY_NUM)];
            // sort arr
            SortingMethod sortMethod = new SortQuick();
            sortMethod.sort(arr);
            printArr(arr);
            // binary search
//            target = 0;
            index[1] = FindFirstTarget.search(arr, target);
            index[2] = FindLastTarget.search(arr, target);
            // search no less or on more than
            index[3] = FindFirstBiggerThanTarget.search(arr, target - 1);
            index[4] = FindLastSmallerThanTarget.search(arr, target - 1);
            //print list
            System.out.print("target: " + target + " index: ");
            printArr(index);
        }

    }

    private static void printArr(int[] arr) {
        System.out.print("[ ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("]");
    }

    private static int[] createRandomArray() {
        Random rd = new Random();
        int[] arr = new int[ARRAY_NUM];
        for (int i = 0; i < ARRAY_NUM; i++) {
            arr[i] = rd.nextInt(INT_RANGE) + 1;
        }
        return arr;
    }
}
